//奖励配置
import React from 'react'
import {message, Space} from "antd";
import EditInput from "../withdrawSetting/component/editInput";
import TheTable from "@/components/TheTable";
import Edit from "./component/edit";
import {getShareConfig,apiShareConfigUpdate} from "@/request/distribution/awardSetting";

export default class AwardSetting extends React.Component {
    state = {
      
        defaultCommissionRate: 0,
        defaultConfigId: '0',
        columns: [
            {
                title: "条件",
                dataIndex: "id",
                key: "id",
                render: (text,record)=>{
                    if (record.type === 1){
                        return '下单人数为区间外的用户数时'
                    }
                    else {
                        return <Space>
                            <span>下单用户数</span>
                            <span>{record.min}</span>
                            <span>~</span>
                            <span>{record.max}</span>
                            <span>人，或GMV达到</span>
                            <span>{React.$transNum(record.gmv, 0, -2)}</span>
                            <span>元</span>
                        </Space>
                    }
                }
            },
            {
                title: "佣金比例",
                dataIndex: "commissionRate",
                key: "commissionRate",
                render: (text,record)=>{
                    return text + '%'
                }
            },
        ],
        dataSource: [],
    }

    componentDidMount() {
        this.getDefaultData()
        this.getDataSource()
    }
    // 获取默认分佣比例
    getDefaultData = () => {
        getShareConfig({type:0}).then(res=>{
            if (res.code === 0){
                let defaultCommissionRate = res.data[0].commissionRate;
                this.setState({
                    defaultCommissionRate,
                    defaultConfigId: res.data[0].configId
                })
            }

        })
    }
    // 获取考核分佣比例
    getDataSource = () => {
        getShareConfig().then(res=>{
            if (res.code === 0){
                let d1 = res.data.filter(item=>item.type===1)
                let d2 = res.data.filter(item=>item.type===2)
                this.setState({
                    dataSource: [...d2,...d1]
                })
            }
        })
    }
    onSave = async (v) => {
        if (v !== this.state.defaultCommissionRate){
            let res = await apiShareConfigUpdate({
                configId: this.state.defaultConfigId,
                commissionRate: v,
                type: 0
            });
            if (res.code === 0){
                message.success('修改成功')
            }
            return res.success
        }else{
            return true
        }
    }
    updateDataSource = () => {
        this.getDataSource()
    }
    render() {
        let {
            defaultCommissionRate,
            columns,
            dataSource,
        } = this.state
        return (
            <div className='p-layout-box commodity-content'>
                <h1>订单分佣比例设置</h1>
                <Space style={{lineHeight: '34px'}}>
                    1. 默认分佣比例设置
                    <EditInput
                        min={0}
                        max={100}
                        precision={0}
                        defaultValue={defaultCommissionRate}
                        addonAfter={'%'}
                        onSave={(v) => this.onSave(v)}
                        haspermi={['distribution:awardSetting:default']}
                    />
                </Space>
                <p>（加入时间未超过15号的高级分享官，默认分佣比例持续2个月。加入时间等于或超过15号的高级分享官
                    默认分佣比例保留3个月）</p>
                <br/>
                <Space direction={'vertical'} >
                    2. 考核分佣比例设置
                    <TheTable
                        columns={columns}
                        dataSource={dataSource}
                        rowKey={'id'}
                    />
                    <Edit update={()=>this.updateDataSource()}/>
                </Space>
            </div>
        )
    }
}
